/**
 * Created by Maibenben on 2017/5/4.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    //清除本地缓存
    localStorage.removeItem("member_id");
    localStorage.removeItem("desc");
    localStorage.removeItem("base");

    var curr = new Date();
    var date = curr.getFullYear() + "-" + (curr.getMonth() + 1) + "-" + (parseInt(curr.getDate()) + parseInt(location.search.split("=")[3]));
    //进入页面显示就诊人信息
    $.get(hosURL + "?mid=" + $.cookie("mid") + "&token=" + $.cookie("token"), function (data) {
        if (data.total == 0) {
            $(".option").hide();
            $(".footer").css("background", "gray");
        } else {
            var refHTML = template("refTem", data);
            $(".people").html(refHTML);
            //添加就诊人
            $(".add").on("click", function () {
                window.location.href = "add-person-reference.html"
            });
            //点击下一步进入确认预约
            $(".footer").click(function () {
                //这里使用localstorage进行本地保存信息
                var member_id = $("#people-list").val();//就诊人的id
                var desc = $(".desc").val();//就诊人的病情描述
                localStorage.setItem("member_id", member_id);
                localStorage.setItem("desc", desc);
                localStorage.setItem("base", base64);

                $.get(checkStateURL + "?m_id=" + member_id + "&token" + $.cookie("token"), function (data) {
                    if (data.state == 1) {
                        window.location.href = "confirm.html";
                    } else if (data.state == 2) {
                        $(".tip").css("display", "block");
                        $(".yes").on("click", function () {
                            window.location.href = "bind-card-confirm.html?id=" + member_id;
                        });
                        $(".no").on("click", function () {
                            window.location.href = "confirm.html";
                        });
                    }
                });

                console.log(desc);
            });
        }
    });
    //文本输入300限制
    $('.desc').on('keyup', function () {
        var textval = $(this).val().length;
        if (textval > 300) {
            $('.current').html(300);
        } else {
            $('.current').html(textval);
        }
        if (textval >= 300) {
            var num = $(this).val().substr(0, 300);
            $(this).val(num);
        }
    });
    //上传图片
    var base64 = [];
    $(".inp").on("change", function () {
        var that = $(this).parent();
        var reader = new FileReader;

        if (this.files[0]) {
            reader.readAsDataURL(this.files[0]);
            reader.onload = function () {
                that[0].style.background = "url(" + reader.result + ") no-repeat center/cover";
                base64.push(reader.result);
            }
        }

    });
});